#include <iostream>
#include <string>
#include <cstring>
#include "../../keyauthcore.h"

// 简单解析JSON结果
void printContractResult(const char* resultJSON) {
    std::cout << "合约结果JSON: " << resultJSON << std::endl;
    
    // 简单提取关键信息（实际应用中应使用JSON库）
    std::string result(resultJSON);
    
    // 提取交易ID
    size_t txIdPos = result.find("\"tx_id\"");
    if (txIdPos != std::string::npos) {
        size_t valueStart = result.find(":", txIdPos) + 1;
        size_t valueEnd = result.find(",", valueStart);
        if (valueEnd != std::string::npos) {
            std::string txId = result.substr(valueStart, valueEnd - valueStart);
            // 去除引号和空格
            txId.erase(0, txId.find_first_not_of(" \""));
            txId.erase(txId.find_last_not_of(" \"") + 1);
            std::cout << "交易ID: " << txId << std::endl;
        }
    }
}

int main() {
    std::cout << "KeyAuth Core 综合示例 (C++)" << std::endl;
    std::cout << "=========================" << std::endl;
    
    // 1. 生成密钥对
    std::cout << "\n[1] 生成密钥对" << std::endl;
    char* pubKey = nullptr;
    char* priKey = nullptr;
    
    if (KeyAuthGenerateKeyPair("SM2", &pubKey, &priKey) == KEYAUTH_SUCCESS) {
        std::cout << "公钥: " << pubKey << std::endl;
        std::cout << "私钥: " << priKey << std::endl;
        
        // 2. 签名与验证
        std::cout << "\n[2] 签名与验证" << std::endl;
        const char* message = "Hello, World!";
        void* signature = nullptr;
        int signatureLen = 0;
        
        if (KeyAuthSign("SM2", priKey, (void*)message, strlen(message), &signature, &signatureLen) == KEYAUTH_SUCCESS) {
            std::cout << "签名成功，长度: " << signatureLen << " 字节" << std::endl;
            
            if (KeyAuthVerify("SM2", pubKey, (void*)message, strlen(message), signature, signatureLen) == KEYAUTH_SUCCESS) {
                std::cout << "签名验证成功" << std::endl;
            } else {
                std::cout << "签名验证失败" << std::endl;
            }
            
            KeyAuthFree(signature);
        }
        
        // 2.1 JSON签名与验证
        std::cout << "\n[2.1] JSON签名与验证" << std::endl;
        const char* jsonData = "{\"name\": \"KeyAuth\", \"version\": \"1.0\", \"features\": [\"签名\", \"验证\", \"安全存储\"]}";
        void* jsonSignature = nullptr;
        int jsonSignatureLen = 0;
        if (KeyAuthSignJSON("SM2", priKey, jsonData, &jsonSignature, &jsonSignatureLen) == KEYAUTH_SUCCESS) {
            std::cout << "JSON签名成功，长度: " << jsonSignatureLen << " 字节" << std::endl;
            if (KeyAuthVerifyJSON("SM2", pubKey, jsonData, jsonSignature, jsonSignatureLen) == KEYAUTH_SUCCESS) {
                std::cout << "JSON签名验证成功" << std::endl;
            } else {
                std::cout << "JSON签名验证失败" << std::endl;
            }
            KeyAuthFree(jsonSignature);
        } else {
            std::cout << "JSON签名失败" << std::endl;
        }

        // 3. 合约查询
        std::cout << "\n[3] 合约查询" << std::endl;
        const char* chainID = "chain1";
        const char* nodeAddr = "127.0.0.1";
        int nodePort = 12301;
        const char* contractName = "contract_name";
        const char* queryMethod = "query_method";
        const char* paramsJSON = "{\"key\":\"value\"}";
        
        char* queryResult = nullptr;
        if (KeyAuthQuery(
            (char*)chainID, (char*)nodeAddr, nodePort, 
            (char*)contractName, (char*)queryMethod, 
            priKey, (char*)paramsJSON, &queryResult) == KEYAUTH_SUCCESS) {
            
            std::cout << "查询合约成功:" << std::endl;
            printContractResult(queryResult);
            KeyAuthFree(queryResult);
        }
        
        // 4. 合约调用
        std::cout << "\n[4] 合约调用" << std::endl;
        const char* invokeMethod = "invoke_method";
        char* invokeResult = nullptr;
        
        if (KeyAuthInvoke(
            (char*)chainID, (char*)nodeAddr, nodePort, 
            (char*)contractName, (char*)invokeMethod, 
            priKey, (char*)paramsJSON, &invokeResult) == KEYAUTH_SUCCESS) {
            
            std::cout << "调用合约成功:" << std::endl;
            printContractResult(invokeResult);
            KeyAuthFree(invokeResult);
        }
        
        // 释放内存
        KeyAuthFree(pubKey);
        KeyAuthFree(priKey);
    } else {
        std::cout << "生成密钥对失败" << std::endl;
    }
    
    std::cout << "\n示例完成!" << std::endl;
    return 0;
} 